﻿/**************************************************************************
创建日期:	2022.6.14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c)  ZhCun.cn

描	述：字符串相关的扩展方法
记	录：
***************************************************************************/
using ASmile.Helpers;
using System;
using System.Globalization;
using System.Linq;

namespace ASmile.Extend
{
    public static class StringExtend
    {
        /// <summary>
        /// 去除所有空（包含回车换行）后分割成数组,如果 separator!=" " 则只替换回车换行,而不替换空字符串
        /// </summary>
        public static string[] SplitByNotEmpty(this string str, params string[] separator)
        {
            if (str.IsEmpty()) return null;
            str = str.Replace("\r", string.Empty);
            str = str.Replace("\n", string.Empty);
            if (!separator.Contains(" "))
            {
                str = str.Replace(" ", string.Empty);
            }
            var rArr = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            return rArr;
        }
        /// <summary>
        /// 去除所有回车换行，不处理空字符串，后分割成数组
        /// </summary>
        public static string[] SplitByNotRN(this string str, params string[] separator)
        {
            if (str.IsEmpty()) return null;
            str = str.Replace("\r", string.Empty);
            str = str.Replace("\n", string.Empty);
            var rArr = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            return rArr;
        }
        /// <summary>
        /// 比较两个字符串，不区分大小写且不计算前后空格
        /// </summary>
        public static bool StringEquals(this string a, string b)
        {
            if (a == null && b == null) return true;
            if (a == null) return false;
            if (b == null) return false;
            return string.Equals(a.Trim().ToUpper(), b.Trim().ToUpper());
        }
        /// <summary>
        /// 指定字符串是否出现在参数字符串中，实现空置比较，同时不区分大小写
        /// </summary>
        public static bool ContainsLike(this string str, string eqStr)
        {
            if (str == eqStr) return true;
            if (str == null) return false;
            return str.ToLower().Contains(eqStr.ToLower());
        }

        /// <summary>
        /// 指定字符串是否在指定大小范围内,如果str为空则返回false，minLen与maxLen都为null 返回false
        /// </summary>
        public static bool IsLenRange(this string str, int? minLen, int? maxLen)
        {
            if (string.IsNullOrWhiteSpace(str)) return false;
            if (minLen == null && maxLen == null) return false;
            if (minLen == null)
            {
                return str.Length <= maxLen;
            }
            if (maxLen == null)
            {
                return str.Length >= minLen;
            }
            return str.Length >= minLen && str.Length <= maxLen;
        }
        /// <summary>
        /// 指定字符串是否在大于最小值,如果str为空则返回false
        /// </summary>
        public static bool IsLenRange(this string str, int? minLen)
        {
            return IsLenRange(str, minLen, null);
        }
        /// <summary>
        /// 判断所有字符串是否为空，只有其中一个为null或空字符串则返回true
        /// </summary>
        public static bool HasEmpty(params string[] strs)
        {
            if (strs == null || strs.Length == 0) return true;
            foreach (var item in strs)
            {
                if (item.IsEmpty()) return true;
            }
            return false;
        }

        /// <summary>
        /// 判断指定字符串是否为空或空字符串
        /// </summary>
        public static bool IsEmpty(this string str)
        {
            if (str == null) return true;
            if (str.Trim().Length == 0) return true;
            return false;
        }
        /// <summary>
        /// 判断是否非空，
        /// </summary>
        public static bool IsNotEmpty(this string str)
        {
            return !IsEmpty(str);
        }
        /// <summary>
        /// 字符串转为 Long
        /// </summary>
        public static long ToLong(this string str)
        {
            if (str.IsEmpty()) return 0;
            long.TryParse(str, out long r);
            return r;
        }
        /// <summary>
        /// 字符串转为int
        /// </summary>
        public static int ToInt(this string str)
        {
            if (str.IsEmpty()) return 0;
            int.TryParse(str, out int r);
            return r;
        }
        /// <summary>
        /// 字符串转为int
        /// </summary>
        public static short ToShort(this string str)
        {
            if (str.IsEmpty()) return -1;
            short.TryParse(str, out short r);
            return r;
        }
        /// <summary>
        /// 字符串转换为 double
        /// </summary>
        public static double ToDouble(this string str)
        {
            double.TryParse(str, out double r);
            return r;
        }
        /// <summary>
        /// 字符串转换为 decimal
        /// </summary>
        public static decimal ToDecimal(this string str)
        {
            decimal.TryParse(str, out decimal r);
            return r;
        }
        /// <summary>
        /// 字符串转换为 float
        /// </summary>
        public static float ToFloat(this string str)
        {
            float.TryParse(str, out float r);
            return r;
        }
        /// <summary>
        /// true 或者 1 返回true，否则返回false
        /// </summary>
        public static bool ToBool(this string str)
        {
            if (str.IsEmpty()) return false;
            return str.StringEquals("true") && str.StringEquals("1");
        }
        /// <summary>
        /// 转换为日期格式
        /// </summary>
        public static DateTime ToDate(this string str, string frmStr = "yyyy-MM-dd")
        {
            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
            dtFormat.ShortDatePattern = frmStr;
            return DateTime.ParseExact(str, frmStr, CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// 通过枚举名称字符串转换枚举类型
        /// </summary>
        public static T ToEnum<T>(this string emName)
        {
            return EnumHelper.StringToEnum<T>(emName);
        }
    }
}